show dbs
use mongodemo
db.createCollection("users")
db.users.insert({
	name:"dafei",age:18
})

//查询集合中所有文档
db.users.find({})
//把带有name=dafei的文档，修改其age值为30
db.users.update(
{name:"dafei"},
{$set:{age:30}},
{
	multi:true
}
)

//修改所有name=dafei的文档，修改其name=大飞，age=20
db.users.updateMany(
{name:"dafei"},
{$set:{name:"大飞",age:20}}
)

//修改所有的文档，修改其name=fei，age=10
db.users.update(
{},
{$set:{name:"fei",age:10}},
{
	multi:true
}
)

//添加一个sex的列
db.users.updateMany(
{},
{$set:{sex:""}}
)
//删除sex的列
db.users.update(
{},
{$unset:{sex:""}},
{multi:true}
)

//删除_id=5f2553088d570000440072f9的文档
db.users.remove(
{_id:ObjectId("5f2553298d570000440072fb")},
{justOnly:true}
)

//删除所有带有name=dafei的文档
db.users.deleteMany(
{name:"dafei"}
)

//删除当前数据库中所有文档
db.users.deleteMany({})

db.users.insert({"id":NumberLong(1),"name":"dafei","age":NumberInt(18)})
db.users.insert({"id":NumberLong(2),"name":"xiaofei","age":NumberInt(20)})
db.users.insert({"id":NumberLong(3),"name":"zhang quan dan","age":NumberInt(33)})
db.users.insert({"id":NumberLong(4),"name":"zhang kun","age":NumberInt(26)})
db.users.insert({"id":NumberLong(6),"name":"cai xv kun","age":NumberInt(29)})
db.users.insert({"id":NumberLong(7),"name":"jia nai liang","age":NumberInt(25)})
db.users.insert({"id":NumberLong(8),"name":"fu rong wang","age":NumberInt(28)})
db.users.insert({"id":NumberLong(9),"name":"wang da","age":NumberInt(31)})
db.users.insert({"id":NumberLong(10),"name":"da wang","age":NumberInt(32)})
db.users.insert({"id":NumberLong(11),"name":"will","age":NumberInt(26)})

//查所有用户
db.users.find({})

//查询所有用户按年龄排序(正序/倒序)
db.users.find({}).sort({age:1})
db.users.find({}).sort({age:-1})

//查询所有用户按年龄和id排序(多列排序)
db.users.find({}).sort({age:1,id:1})

//按照年龄降序排列，查询第2页，每页显示3个
db.users.find({}).sort({age:-1}).skip(3).limit(3)

//查询age > 30的用户
db.users.find({age:{$gt:30}})

//查询名字为 dafei 或xiaofei用户
db.users.find({name:{$in:["dafei","xiaofei"]}})

//判断判断指定name列是否存在
db.users.find({name:{$exists:true}})

//查年龄在28 到 30间的用户信息
db.users.find(
{
	$and:[{age:{$gte:28}},{age:{$lte:30}}]
}
)
db.users.find(
{age:{$gte:28,$lte:30}}
)

查看年龄小于28或者年龄大于30用户信息
db.users.find(
{
	$or:[{age:{$lt:28}},{age:{$gt:30}}]
}
)

查看年龄等于28或者等于30用户信息
db.users.find(
{
	age:{$in:[28,30]}
}
)
db.users.find(
{
	$or:[{age:28},{age:30}]
}
)

查询name带有fei的用户信息
db.users.find(
{name:/fei/}
)
查name中包含fei字样，并且年龄在28 到 30间的用户信息
db.users.find(
{
	$and:[{name:/fei/},{age:{$gte:28}},{age:{$lte:30}}]
}
)

给所有的数据添加数组字段hobby
db.users.update(
{},
	{$set:{habby:[]}},
{
	multi:true
}
)

给name等于dafei的用户添加java 爱好
db.users.update(
{name:"dafei"},
{$push:{habby:"java"}}
)

给数组habby字段添加数据， 不允许数据元素重复
db.users.update(
{name:"dafei"},
{$addToSet:{habby:"c"}}
)

给所有的数据添加数组字段hobby
db.users.update(
{},
{$addToSet:{habby:{$each:["java","c","c++"]}}},
{multi:true}
)

需求删除name=dafei用户最后一个兴趣
db.users.update(
{name:"dafei"},
{$pop:{habby:1}},
)

需求删除name=dafei用户第一个兴趣
db.users.update(
{name:"dafei"},
{$pop:{habby:-1}}
)

删除name=dafei用户java兴趣选项
db.users.update(
{name:"dafei"},
{$pull:{habby:"java"}}
)

将name=dafei的用户索引为1的兴趣改为 go
db.users.update(
{name:"dafei"},
{$set:{"habby.1":"java"}}
)

将name=dafei的用户 c 兴趣 改为 c#
db.users.update(
{name:"dafei",habby:"c"},
{$set:{"habby.$":"c#"}}
)